package com.csun.cmny.provider.security;

import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.HuaLongService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.provider.service.WeiXinService;
import com.csun.cmny.security.core.SecurityUser;
import net.sf.json.JSONObject;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2018-11-22 13:35
 * @Description:
 */
@Component
public class UacUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UacUserService uacUserService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private HuaLongService huaLongService;
    @Resource
    private WeiXinService weiXinService;

    private static final String HEADER_LANGUAGE = "Accept-Language";

    /**
     * Load user by username user details.
     *
     * @param jsonStr the json str
     *
     * @return the user details
     */
    @Override
    public UserDetails loadUserByUsername(String jsonStr) {

        /**
         * @date    2020-10-23
         * @author  chisj
         * @desc    适配华隆登录
         */
        if (isJson(jsonStr)) {
            JSONObject jsonObject = JSONObject.fromObject(jsonStr);
            String type = jsonObject.getString("type");
            if ("hualong".equals(type)) {
                String account = jsonObject.getString("account");
                String password = jsonObject.getString("password");
                String role =  jsonObject.getString("role");

                UacUser user = huaLongService.login(account, password, role);

                Collection<GrantedAuthority>grantedAuthorities = uacUserService.loadUserAuthorities(user.getId());
                SecurityUser securityUser = new SecurityUser(user.getId(), user.getLoginName(), user.getLoginPwd(),
                        user.getUserName(), user.getGroupId(), user.getGroupName(), user.getStatus(), grantedAuthorities);
                return securityUser;
            }
            if ("wx".equals(type)) {
                String code = jsonObject.getString("code");
                String nickName = jsonObject.getString("nickName");
                String avatar =  jsonObject.getString("avatar");
                String gender =  jsonObject.getString("gender");
                String city =  jsonObject.getString("city");
                String application = jsonObject.getString("application");

                UacUser user = weiXinService.login(application, code, nickName, avatar, gender, city);

                Collection<GrantedAuthority>grantedAuthorities = uacUserService.loadUserAuthorities(user.getId());
                SecurityUser securityUser = new SecurityUser(user.getId(), user.getLoginName(), user.getLoginPwd(),
                        user.getUserName(), user.getGroupId(), user.getGroupName(), user.getStatus(), grantedAuthorities);
                return securityUser;
            }

        } else {
            Collection<GrantedAuthority> grantedAuthorities;
            UacUser user = uacUserService.findByLoginName(jsonStr);
            if (user == null) {
                throw new BadCredentialsException(cmnyMessageSource.getMessage("user.not.exist", null));
            }
            user = uacUserService.findUserInfoByUserId(user.getId());
            grantedAuthorities = uacUserService.loadUserAuthorities(user.getId());
            SecurityUser securityUser = new SecurityUser(user.getId(), user.getLoginName(), user.getLoginPwd(),
                    user.getUserName(), user.getGroupId(), user.getGroupName(), user.getStatus(), grantedAuthorities);
            return securityUser;
        }

        return null;
    }

    public static boolean isJson(String content) {

        try {
            JSONObject.fromObject(content);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
